define(['./../service/mapInstance', './../service/webSocket/webSocketEmitter', './../service/webSocket/webSocketReceiver'], function() {
    var app = angular.module('App.home');
    app.directive('autoSurveyControlContainer', ['mapInstance', 'webSocketEmitter', 'webSocketReceiver',
        function(mapInstance, webSocketEmitter, webSocketReceiver) {
            return {
                restrict: 'EAC',
                link: function(scope, el, attrs) {
                    var map = mapInstance.getMap();
                    var prjName = map.autoSurveyPrjName;
                    var buffer = webSocketReceiver.buffer;
                    scope.startSurvey = function() {
                        if (buffer.fnBuffer.length > 0 && buffer.suspending) {
                            buffer.start();
                        } else if (buffer.fnBuffer.length === 0) {
                            buffer.suspending = false;
                            webSocketEmitter.StartSurvey(prjName);
                        }
                    };
                    scope.pauseSurvey = function() {
                        if (buffer.fnBuffer.length === 0) {
                            return;
                        } else {
                            buffer.pause();
                        }
                    };
                    scope.stopSurvey = function() {
                        webSocketEmitter.StopSurvey(prjName);
                        buffer.cancel();
                    };

                    scope.$on('removeAutoSurveyControl', function() {
                        buffer.cancel();
                        scope.$destroy(); //删除该指令的所有缓存
                    });
                },
                templateUrl: 'partials/autoSurveyControlContainer.html'
            };
        }
    ]);

});
